package com.total.reporting.presentation;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.total.reporting.dao.Item;
import com.total.reporting.dao.RefList;
import com.total.reporting.dao.HibernateUtil;


public class RefListController {
	
	public synchronized static RefList getRefListById(String id) {
		/*List<RefList> refLists = getRefLists("id=" + id, 0, 10);
		if (refLists.size() == 0)
			return null;
		return refLists.get(0);*/
		Session session = HibernateUtil.currentSession();
		return (RefList) session.get(RefList.class, Integer.parseInt(id));
	}

	public synchronized static List<RefList> getRefLists(String whereClause,
			int pageNumber, int pageSize) {

		Session session = HibernateUtil.currentSession();
		String where = "";

		if (whereClause != null && whereClause.length()>0){
			where = "where " + whereClause;
		}
		String sql = " select * from ref_list " + where + " order by type,order_num,name";
		SQLQuery query = session.createSQLQuery(sql);
		query.addEntity(RefList.class);
		query.setFirstResult((pageNumber - 1) * pageSize);
		query.setMaxResults(pageSize);
		List<RefList> users = query.list();

		return users;

	}
	
	
	public synchronized static String getRefListsArrayGridData(String whereClause) {
		List<RefList>list=getRefLists(whereClause, 0, 1000000);
		Iterator<RefList> it = list.iterator();
		StringBuffer sb = new StringBuffer();
		sb.append("var mydata = [");
		while (it.hasNext()) {
			RefList aRefList = it.next();
			
			sb.append("{id:\""+aRefList.getId()+"\",");
			sb.append("name:\""+aRefList.getName()+"\",");	
			sb.append("type:\""+aRefList.getType()+"\",");	
			sb.append("parentType:\""+aRefList.getParentType()+"\",");
			sb.append("orderNum:\""+aRefList.getOrderNum()+"\"");	
			sb.append("},");
		}
		sb.delete(sb.length()-1, sb.length());
		  sb.append("]");
		//  System.out.println(sb.toString());
		  return sb.toString();
	}
	
	public synchronized static String getRefListsxHtmlOptions(HttpSession session,String type,String currentValue) {
		String options=(String)session.getServletContext().getAttribute("list."+type);
		if(options==null){
			options=getRefListsHtmlOptions("type='"+type+"'",currentValue);
			//session.getServletContext().setAttribute("list."+type,options);
		}
		
		
		
		return options;
	}
	
	public synchronized static String getRefListsHtmlOptions(String whereClause,String currentValue) {
		Iterator<RefList> it = getRefLists(whereClause, 0, 1000000).iterator();
		StringBuffer sb = new StringBuffer();
		// sb.append("<table class='table'><tr class='tableHeader'>");
		// sb.append("<td>IGG</td><td>name</td><td>surname</td><td>entity</td><td>device token</td>");
		// sb.append("</tr>");
		
		while (it.hasNext()) {
			RefList aRefList= it.next();
			String selected="";
			if(currentValue!=null && currentValue.equals(aRefList.getName()))
				selected="selected='selected'";			
			sb.append("<option "+selected+">");
			sb.append(aRefList.getName());
			sb.append("</option>");
		}
		
		return sb.toString();
	}

	
	public synchronized static String getRefListsHtmlTable(){
		Iterator<RefList> it = getRefLists(null, 0, 1000000).iterator();
		StringBuffer sb = new StringBuffer();
		
		sb.append("<table class='alertTable' border='1' ><tr class='tableHeader'>");
		sb.append("<td>name</td><td width='30px'>type</td><td>order</td>");
		sb.append("</tr>");
		while (it.hasNext()) {
			sb.append("<tr class='tableRow'>");
			RefList refList = it.next();
			sb.append(
					"</td><td width='30px' class='labelList' ><a href='javascript:getRefListById(\""+refList.getId()+"\")'>" +  refList.getName()+"</a>"
					+ "</td><td width='30px' class='labelList'>" + refList.getType()
					+ "</td><td width='20px' class='labelList'>" + refList.getOrderNum());

			sb.append("</tr>");
		}
		sb.append("</table>");
		
		return sb.toString();
		
	}
	
	public synchronized static void updateRefList(HttpSession httpSession,RefList refList) {
		try {
			httpSession.getServletContext().removeAttribute("list."+refList.getType());
			Session session = HibernateUtil.currentSession();
			Transaction transaction = session.beginTransaction();
			RefList refListOld = (RefList) session.get(RefList.class, new Integer(
					refList.getId()));

			session.evict(refListOld);
			session.saveOrUpdate(refList);
			transaction.commit();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	public synchronized static int saveRefList(HttpSession httpSession,RefList refList) {
		try {
			httpSession.getServletContext().removeAttribute("list."+refList.getType());
			Session session = HibernateUtil.currentSession();
			return (Integer) session.save(refList);

		} catch (Exception e) {
			e.printStackTrace();
			return -999;
		}

	}

	public synchronized static void delete(HttpSession httpSession,RefList refList ) {
		try {
			httpSession.getServletContext().removeAttribute("list."+refList.getType());
			Session session = HibernateUtil.currentSession();
			Transaction transaction = session.beginTransaction();
			RefList refListOld = (RefList) session.get(RefList.class,
					new Integer(refList.getId()));

			session.evict(refListOld);
			session.delete(refList);
			transaction.commit();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	
	

}
